var cord_x, coord_y, Direccion_pais_id, Direccion_pronvincia_id, Direccion_ciudad_id;
$(function() {
    cord_x = cord_x ? cord_x : (0.346024);
    cord_y = cord_y ? cord_y : (-78.119574);
    Direccion_pais_id = $("#Direccion_pais_id");
    Direccion_provincia_id = $("#Direccion_provincia_id");
    Direccion_ciudad_id = $("#Direccion_ciudad_id");
    initializeMapDiv(cord_x, cord_y, 'map-canvas-creacion', "#Direccion_coord_x", "#Direccion_coord_y", true);
    Direccion_pais_id.select2({
        placeholder: "Seleccione un Pais.",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: baseUrl + "crm/pais/ajaxListSelect2Pais",
            type: "get",
            dataType: 'json',
            data: function(term, page) {
                return {
                    search_value: term
                };
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
    });
    Direccion_provincia_id.select2({
        placeholder: "Seleccione una Provincia.",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: baseUrl + "crm/provincia/ajaxListSelect2Provincias",
            type: "get",
            dataType: 'json',
            data: function(term, page) {
                return {
                    pais_id: Direccion_pais_id.val() != "" ? Direccion_pais_id.val() : null,
                    search_value: term
                };
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
    });
    Direccion_ciudad_id.select2({
        placeholder: "Seleccione una Ciudad.",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: baseUrl + "crm/ciudad/ajaxListSelect2Ciudades",
            type: "get",
            dataType: 'json',
            data: function(term, page) {
                return {
                    provincia_id: Direccion_provincia_id.val() != "" ? Direccion_provincia_id.val() : null,
                    search_value: term
                };
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
    });
});
function AjaxGestionModalFormWizardDireccion($form, CallBack) {
    var form = $($form);
    var settings = form.data('settings');
    settings.submitting = true;
    $.fn.yiiactiveform.validate(form, function(messages) {

        $.each(messages, function() {
        });
        if ($.isEmptyObject(messages)) {
            $.each(settings.attributes, function() {
                $.fn.yiiactiveform.updateInput(this, messages, form);
            });
            AjaxGuardarModalFormWizardDireccion(true, $form, CallBack);
        }
        else {
            settings = form.data('settings'),
                    $.each(settings.attributes, function() {
                        $.fn.yiiactiveform.updateInput(this, messages, form);
                    });
            DesBloquearBotonesModal($form, 'Agregar dirección', 'saveDireccion');
        }
    });
}

function ajaxSaveDireccion($form_id) {

    ajaxValidarFormulario({
        formId: $form_id,
        beforeCall: function() {
//            btn_save_modal.setProgress(0.6);
        },
        successCall: function(data) {
            if (data.success) {
//                habilitarPaneles();
                document.location.href = baseUrl + 'crm/entidad/view/id/' + $('#Direccion_entidad_id').val();

            }
        },
        errorCall: function(data) {
            DesBloquearBotonesModal($form_id, ' Crear', 'ajaxSave');
        }
    });
}
function AjaxGuardarModalFormWizardDireccion(verificador, Formulario, callBack)
{
    if (verificador)
    {
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: $(Formulario).attr('action'),
            data: $(Formulario).serialize(),
            beforeSend: function(xhr) {
            },
            success: function(data) {
                if (data.success) {
                    document.location.href = baseUrl + 'crm/entidad/view/id/' + $('#Direccion_entidad_id').val();
                } else {
                    DesBloquearBotonesModal(Formulario, 'Agregar dirección', 'saveDireccion');
                    bootbox.alert(data.message);
                }
            }
        });
    }


}

function AjaxLoadData(lista, lista_actualizar, url, params)
{
    $('#s2id_' + lista_actualizar + ' a span').html('');
    AjaxGetData(baseUrl + url,
            params, function(data) {
                $("#" + lista_actualizar).html(data);
                $('#s2id_' + lista_actualizar + ' a span').html($("#" + lista_actualizar + " option[em='p']").html());
                $('span.select2-arrow').html('<b role="presentation"></b>');
                $("option[em='p']").val(null);
            });
}
function AjaxGetData(url, data, callBack)
{
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function(data) {
            callBack(data);
        }
    });
}
